package autohome.wz.lib;

import autohome.wz.model.JsonResult;
import autohome.wz.model.WdTopicReplySpider;
import autohome.wz.model.WdTopicSpider;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author yanwei.yang on 2017-10-19 17:22
 **/
public class WenDaTopicHelper {

    private static Logger logger = Logger.getLogger(WenDaTopicHelper.class);


    /**
     * 问答主贴入库
     *
     * @param model 问答主贴model
     * @return true 成功 false失败
     */
    public static boolean addTopic(WdTopicSpider model) {
        boolean result = false;
        String url = Common.getConfigCommon("url.wenda.addTopicSpider");
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("topicId", model.getTopic_id());
        map.put("title", model.getTitle());
        map.put("brandId", model.getBrand_id());
        map.put("brandName", model.getBrand_name());
        map.put("seriesId", model.getSeries_id());
        map.put("seriesName", model.getSeries_name());
        map.put("class1Id", model.getClass1_id());
        map.put("class1Name", model.getClass1_name());
        map.put("class2Id", model.getClass2_id());
        map.put("class2Name", model.getClass2_name());
        map.put("memberId", model.getMember_id());
        map.put("memberName", model.getMember_name());
        map.put("memberPic", model.getMember_pic());
        map.put("topicContent", model.getTopic_content());
        map.put("confirmreplyId", model.getConfirmreply_id());
        map.put("isconfirm", model.isconfirm());
        map.put("replycount", model.getReplycount());
        map.put("replyState", 0);
        map.put("checkState", 0);
        map.put("postdate", model.getPostdate());
        try {
            String apiResult = HttpRequest.sendPost(url, map);
            logger.error("ApiResult:" + apiResult);
            if (apiResult.length() > 0) {
                Gson gson = new Gson();
                JsonResult jsonResult = gson.fromJson(apiResult, JsonResult.class);
                if (jsonResult.getReturncode() == 0) {
                    result = true;
                    logger.debug(model.getTitle() + "--success");
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }


        return result;
    }


    /**
     * 问答回复入库
     *
     * @param model 回复model
     * @return true 成功 false失败
     */

    public static boolean addTopicReply(WdTopicReplySpider model) {
        boolean result = false;
        String url = Common.getConfigCommon("url.wenda.addTopicReplySpider");
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("topicId", model.getTopic_id());
        map.put("replyId", model.getReply_id());
        map.put("rMemberid", model.getR_memberid());
        map.put("rMemberName", model.getR_member_name());
        map.put("rMemberPic", model.getR_member_pic());
        map.put("rContent", model.getR_content());
        map.put("usefulCount", model.getUseful_count());
        map.put("replydate", model.getReplydate());
        map.put("targetreplyid", model.getTargetreplyid());
        try {
            String apiResult = HttpRequest.sendPost(url, map);
            logger.error("addTopicReply-result:" + apiResult);
            if (apiResult.length() > 0) {
                Gson gson = new Gson();
                JsonResult jsonResult = gson.fromJson(apiResult, JsonResult.class);
                if (jsonResult.getReturncode() == 0) {
                    result = true;
                }
            }
        } catch (Exception e) {
            logger.error("addTopicReply:" + e.getMessage());
        }
        return result;

    }

}
